﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      CIS  Overseas AreaManagementHelper
// Created By:     Samara Jiang (samaraj@wicresoft.com)
// Created Date:   2011-1-27
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using ASBCC.Database.EntityFramework;

namespace ASBCC.Business.CIS
{
    /// <summary>
    ///  CIS  Overseas AreaManagement  Helper
    /// </summary>
    public static class AreaManagementHelper
    {
        /// <summary>
        /// 通过AreaID查询Countries
        /// </summary>
        public static IEnumerable GetCountriesWithAreaRelation(int areaID)
        {
            using (CCEntities dc = new CCEntities())
            {
                var area = dc.Areas.FirstOrDefault(p => p.AreaID == areaID);
                var countries = dc.Countries.Where(p => p.Disabled == false).ToArray();
                area.Countries.Load();
                var source = from p in countries
                             select new
                             {
                                 p.CountryID,
                                 p.Name,
                                 p.Description,
                                 ContainsArea = area.Countries.Contains(p) ? "checked" : "",
                             };
                return source.ToArray();
            }
        }

       
        /// <summary>
        /// 为Area添加Country
        /// </summary>
        public static void OnAreaInserting(ObjectContext dc, Area area, string countryList)
        {
           

            var countries =dc.CreateQuery<Country>("[Countries]").Where(p => p.Disabled == false);

            if (!String.IsNullOrEmpty(countryList))
            {

                foreach (var country in countryList.Split(','))
                {
                    int countryID = int.Parse(country);

                    var CountryInArea = countries.First(p => p.CountryID == countryID);

                    area.Countries.Add(CountryInArea);

                }

            }
        }

        /// <summary>
        /// 为Area修改Country
        /// </summary>
        public static void OnAreaUpdating(ObjectContext dc, Area area, string countryList)
        {
            area.Countries.Load();
            area.Countries.Clear();

            OnAreaInserting(dc, area, countryList);

        }


        /// <summary>
        /// 将一个area的激活状态设为未激活
        /// </summary>
        public static void SetAreaDisabled(int areaID)
        {
            using (var dc = new CCEntities())
            {
                dc.Areas.FirstOrDefault(p => p.AreaID == areaID).Disabled = true;
                dc.SaveChanges();
            }
        }

        /// <summary>
        /// 根据当前ID获取Name
        /// </summary>
        public static IEnumerable getName(int entityID)
        {
            using (var dc = new CCEntities())
            {
                var name = dc.Areas.FirstOrDefault(p => p.AreaID == entityID).Name;
                return name;
            }
        }

        public static void InternalUpdateArea(ObjectContext dc, Area area, string countryList)
        {
            if (countryList != null)
            {
                var countries = dc.CreateQuery<Country>("[Countries]");
                foreach (var country in countryList.Split(','))
                {
                    int countryID = int.Parse(country);
                    var entity = countries.First(p => p.CountryID == countryID);
                    area.Countries.Add(entity);
                }
            }

        }

    }
}
